<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      color: #c0ddfa;
    }
  </style>
</head>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>


<body>
  <div id="app">
    <template>
      <el-table :data="tableData" stripe style="width: 100%" height="385px" border ref="rw_table"
        @mouseenter.native="mouseEnter" @mouseleave.native="mouseLeave">
        <el-table-column prop="date" label="日期" width="180">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="address" label="地址">
        </el-table-column>
      </el-table>
    </template>
  </div>

  <script>
    let rolltimer = '' // 自动滚动的定时任务 

    // 模拟的表格数据
    let assume_data = [];
    for (let i = 1; i <= 10; i++) {
      assume_data.push({
        date: '2016-05-0' + i,
        name: '王小虎' + i,
        address: '上海市普陀区金沙江路 151' + i + ' 弄'
      })
    }


    let vm = new Vue({
      el: '#app',
      data: {
        tableData: assume_data,
        //  滚动时间，滚动间距 
        rollTime: 5,
        rollPx: 1,
      },
      mounted() {
        this.autoRoll()
      },
      methods: {

        // 设置自动滚动
        autoRoll(stop) {
          if (stop) return clearInterval(rolltimer)
          // 拿到表格挂载后的真实DOM
          const table = this.$refs.rw_table
          // 拿到表格中承载数据的div元素
          const divData = table.bodyWrapper
          // 拿到元素后，对元素进行定时增加距离顶部距离，实现滚动效果
          rolltimer = setInterval(() => {
            // 元素自增距离顶部像素
            divData.scrollTop += this.rollPx
            // 判断元素是否滚动到底部(可视高度+距离顶部=整个高度)
            console.log(Math.ceil(divData.clientHeight + divData.scrollTop), divData.scrollHeight);
            if (Math.ceil(divData.clientHeight + divData.scrollTop) == divData.scrollHeight) {
              // 重置table距离顶部距离
              divData.scrollTop = 0
            }
          }, this.rollTime * 10)
        },

        // 鼠标进入
        mouseEnter(time) {
          // 鼠标进入停止滚动和切换的定时任务
          this.autoRoll(true)
        },
        // 鼠标离开
        mouseLeave() {
          // 开启
          this.autoRoll()
        },
      },
    })

  </script>
</body>

</html>